﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LP580.CG;

public partial class Manage_UserAccountList : System.Web.UI.Page
{
    private string act = Utils.RQ_str("act");
    private int id = Utils.RQ_int("id");
    private string searchKey = Utils.RQ_str("sk");
    private int pageIndex = Utils.RQ_int("pi");
    private int pageSize = Utils.RQ_int("pz");
    protected void Page_Load(object sender, EventArgs e)
    {
        if (pageSize == 0) pageSize = 10;
        switch (act)
        {
            case "del":
                Delete();
                break;
        }
        if (!IsPostBack)
            bindData();
        bindContrl();
    }


    private void Delete()
    {
        clsUserAccount.Delete(id);
    }

    private void bindContrl()
    {

    }

    private void bindData()
    {
        //string code = "";
        //if (id != 0)
        //{
        //    DBase db = new DBase();
        //    System.Data.IDataReader dr = db.ExecuteReader("select GiftProductUpClassName,GiftProductUpClassID from tbl_UserAccount where UserAccountID=" + id.ToString());
        //    if (dr.Read())
        //    {
        //        ClassUrl.Text = string.Format("-[<a href='?id={1}'>{0}</a>]", dr[0], dr[1]);
        //    }
        //}
        PagedDataSource pds = new PagedDataSource();
        pds.DataSource = clsUserAccount.GetCollection();

        pds.PageSize = pageSize;
        pds.CurrentPageIndex = (pageIndex - 1 < 0) ? 0 : (pageIndex - 1);
        pds.AllowPaging = true;

        string baseUrl = "UserAccountList.aspx?sk=" + searchKey;
        ltPage.Text = LvEasyPager.Create(baseUrl + "&pi={0}&pz={1}", pageIndex, pageSize, pds.DataSourceCount);

        gvDatalist.DataSource = pds;
        gvDatalist.PageIndex = pageIndex;
        gvDatalist.DataBind();
    }

    protected void lkbtnBatchDel_Click(object sender, EventArgs e)
    {
        List<clsUserAccount> ls = getDelList();
        bool isErr = false;
        string ErrMsg = "以下不可删除：\\n\\n";
        if (ls.Count > 0)
        {
            foreach (clsUserAccount s in ls)
            {
                bool CanDel = true;
                if (CanDel) //可删除
                {
                    clsUserAccount.Delete(s.UserID);
                }
                else
                {
                    isErr = true;
                    ErrMsg += " " + s.UserName;
                }
            }
        }
        string url = "UserAccountList.aspx?pi=" + pageIndex + "&pz=" + pageSize;
        if (isErr)
        {
            Utils.WrAlertGo(ErrMsg, url);
        }
        else
        {
            Response.Redirect(url, true);
        }
    }

    private List<clsUserAccount> getDelList()
    {
        string ids = Utils.RF_str("ids");
        List<clsUserAccount> ls = new List<clsUserAccount>();
        if (ids != "")
        {
            string[] arrID = ids.Split(',');
            int arrLen = arrID.Length;
            for (int i = 0; i < arrLen; i++)
            {
                int id = 0;
                if (!int.TryParse(arrID[i].Trim(), out id))
                    id = 0;
                if (id > 0)
                {
                    clsUserAccount s = new clsUserAccount(id);
                    if (s.UserID > 0)
                        ls.Add(s);
                }
            }
        }
        return ls;
    }
}